<?php

class user_notifyModel extends Model 
{
    
    public function getInfo($uid) {
        $resultData = [
            'up' => [
                'num' => 0,
                'type' => notifyModel::TYPE_UP
            ],
            'collect' => [
                'num' => 0,
                'type' => notifyModel::TYPE_COLLECT
            ],
            'comment' => [
                'num' => 0,
                'type' => notifyModel::TYPE_COMMENT
            ],
            'follow' => [
                'num' => 0,
                'type' => notifyModel::TYPE_FOLLOW
            ],
            'notice' => [
                'num' => 0,
                'type' => notifyModel::TYPE_NOTICE
            ]
        ];
        if(!$userNotify = $this->getOne('uid=?', $uid)) {
            return $resultData;
        }
        $resultData['up']['num'] = intval($userNotify['up_num']);
        $resultData['collect']['num'] = intval($userNotify['collect_num']);
        $resultData['comment']['num'] = intval($userNotify['comment_num']);
        $resultData['follow']['num'] = intval($userNotify['follow_num']);
        $resultData['notice']['num'] = intval($userNotify['notice_num']);
        return $resultData;
    }
    
    //添加消息方法;
    public static function addNotify($uid, $type, $typeId, $fuserInfo, $content='') {
        $data = [
            'uid'=>$uid,
            'type' => $type,
            'type_id' => $typeId,
            'fuid' => $fuserInfo['uid'],
            'nickname' => $fuserInfo['nickname'],
            'avatar' => $fuserInfo['avatar'],
            'content' => $content,
            'add_time' => time()
        ];
        if($type == notifyModel::TYPE_UP) { 
            $data['style_type'] = notifyModel::STYLE_TYPE_UP_AND_COLLECT;
            Model::getInstance()->execute("INSERT INTO wf_user_notify (uid,up_num,collect_num,comment_num,follow_num,notice_num) VALUES ({$uid},1,0,0,0,0) ON DUPLICATE KEY UPDATE up_num=up_num+1");
        }elseif($type == notifyModel::TYPE_COLLECT) {
            $data['style_type'] = notifyModel::STYLE_TYPE_UP_AND_COLLECT;
            Model::getInstance()->execute("INSERT INTO wf_user_notify (uid,up_num,collect_num,comment_num,follow_num,notice_num) VALUES ({$uid},0,1,0,0,0) ON DUPLICATE KEY UPDATE collect_num=collect_num+1");
        }elseif($type== notifyModel::TYPE_COMMENT) {
            $data['style_type'] = notifyModel::STYLE_TYPE_COMMENT;
            Model::getInstance()->execute("INSERT INTO wf_user_notify (uid,up_num,collect_num,comment_num,follow_num,notice_num) VALUES ({$uid},0,0,1,0,0) ON DUPLICATE KEY UPDATE comment_num=comment_num+1");
        }elseif($type== notifyModel::TYPE_FOLLOW) {
            $data['style_type'] = notifyModel::STYLE_TYPE_FOLLOW;
            Model::getInstance()->execute("INSERT INTO wf_user_notify (uid,up_num,collect_num,comment_num,follow_num,notice_num) VALUES ({$uid},0,0,0,1,0) ON DUPLICATE KEY UPDATE follow_num=follow_num+1");
        }elseif($type== notifyModel::TYPE_NOTICE) {
            $data['style_type'] = notifyModel::STYLE_TYPE_NOTICE;
            Model::getInstance()->execute("INSERT INTO wf_user_notify (uid,up_num,collect_num,comment_num,follow_num,notice_num) VALUES ({$uid},0,0,0,0,1) ON DUPLICATE KEY UPDATE notice_num=notice_num+1");
        }else{
            return true;
        }
        Model::getInstance()->setTableName('wf_notify')->insert($data);
        return true;
    }
    
    public function clear($uid, $type) {
        if($type== notifyModel::TYPE_UP||$type==notifyModel::TYPE_COLLECT) {
            $this->update([
                'up_num' => 0,
                'collect_num' => 0,
            ], 'uid=?', $uid);
        }elseif($type== notifyModel::TYPE_COMMENT) {
            $this->update([
                'comment_num' => 0
            ], 'uid=?', $uid);
        }elseif($type== notifyModel::TYPE_FOLLOW) {
            $this->update([
                'follow_num' => 0
            ], 'uid=?', $uid);
        }elseif($type== notifyModel::TYPE_NOTICE) {
            $this->update([
                'notice_num' => 0
            ], 'uid=?', $uid);
        }
        return true;
    }
}